<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="费用名称" prop="costName">
      <el-input v-model="dataForm.costName" placeholder="费用名称"></el-input>
    </el-form-item>
      <el-form-item label="费用产生环境" prop="costEnvironment">
        <el-input v-model="dataForm.costEnvironment" placeholder="费用产生环境"></el-input>
      </el-form-item>
    <el-form-item label="费用金额" prop="costMoney">
      <el-input-number v-model="dataForm.costMoney" placeholder="费用金额"></el-input-number>
    </el-form-item>
    <el-form-item label="费用类型" prop="costType">
      <el-select v-model="dataForm.costType" placeholder="请选择">
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value*1">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="费用对应环境类型" prop="environmentType">
      <el-select v-model="dataForm.environmentType" clearable >
        <el-option
          v-for="item in optionsType"
          :key="item.value"
          :label="item.label"
          :value="item.value*1">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="费用产生时间" prop="costTime">
      <el-date-picker v-model="dataForm.costTime" placeholder="费用产生时间"></el-date-picker>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        optionsType: [
          {
            value: '1',
            label: '冷库'
          }, {
            value: '2',
            label: '冷链车'
          }
        ],
        options: [],
        visible: false,
        dataForm: {
          id: 0,
          costName: '',
          costMoney: '',
          costType: '',
          environmentType: '',
          costTime: '',
          costStatus: '',
          costEnvironment: ''
        },
        dataRule: {
          costName: [
            { required: true, message: '费用名称不能为空', trigger: 'blur' }
          ],
          costMoney: [
            { required: true, message: '费用金额不能为空', trigger: 'blur' }
          ],
          costType: [
            { required: true, message: '费用类型不能为空', trigger: 'blur' }
          ],
          environmentType: [
            { required: true, message: '费用对应环境类型不能为空', trigger: 'blur' }
          ],
          costTime: [
            { required: true, message: '费用产生时间不能为空', trigger: 'blur' }
          ],
          costEnvironment: [
            { required: true, message: '费用产生环境不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/app/entexpendituremanagement/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.costName = data.entExpenditureManagement.costName
                this.dataForm.costMoney = data.entExpenditureManagement.costMoney
                this.dataForm.costType = data.entExpenditureManagement.costType
                this.dataForm.environmentType = data.entExpenditureManagement.environmentType
                this.dataForm.costTime = data.entExpenditureManagement.costTime
                this.dataForm.costStatus = data.entExpenditureManagement.costStatus
                this.dataForm.costEnvironment = data.entExpenditureManagement.costEnvironment
              }
            })
          }
        })
        this.$http({
          url: this.$http.adornUrl(`/app/dictitem/getListById/cost_type`),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.options = data.list
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/app/entexpendituremanagement/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'costName': this.dataForm.costName,
                'costMoney': this.dataForm.costMoney,
                'costType': this.dataForm.costType,
                'environmentType': this.dataForm.environmentType,
                'costTime': this.dataForm.costTime,
                'costStatus': this.dataForm.costStatus,
                'costEnvironment': this.dataForm.costEnvironment
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
